<?php
/* MyMeeting is an online meeting management software.
 * Copyright (C) 2009 Government Of Malaysia
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * @author: Abdullah Zainul Abidin, Nuhaa All Bakry
 *          Eavay Javay Barnad, Sarogini Muniyandi, Teow Jit Huan
 *
 */

class Wfmodel extends AppModel {

    /**
     * Defining the name of model
     *
     */
    var $name = 'Wfmodel';

    //The Associations below have been created with all possible keys, those that are not needed can be removed
    /**
     * Building assosiations betweeen models 
     *
     */
    var $belongsTo = array(
        'Committee' => array(
            'className' => 'Committee',
            'foreignKey' => 'committee_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );
    
    function getRules($committee_id,$models) {
        $this->unbindModel(array('belongsTo'=>array('Committee')));
        $rules = $this->find('all',array(
            'conditions'=>array('model'=>$models,'committee_id'=>$committee_id),
        ));
        return $rules;
    }
    
    function defaultpermission($role){
        if( isset($role) && $role==1){
            return array(
                'Committee'=>array('Create'=>1,'Edit'=>1,'Delete'=>1),
                'Committeetodo'=>array('Create'=>1,'Edit'=>1,'Delete'=>1),
                'Wfmodel'=>array('Edit'=>1),
                'Announcement'=>array('Create'=>1,'Edit'=>1,'Delete'=>1),
                'Template'=>array('View'=>1,'Edit'=>1),
                'Membership'=>array('Create'=>1,'Edit'=>1,'Delete'=>1),
                'Group'=>array('Create'=>1,'Edit'=>1,'Delete'=>1),
                'Meeting'=>array('Create'=>1,'View'=>1,'Edit'=>1,'Delete'=>1),
                'Meeting -Invited'=>array('View'=>1,'Edit'=>1,'Delete'=>1),
                'Meeting -Attended'=>array('View'=>1,'Edit'=>1,'Delete'=>1),
                'Attendance'=>array('View'=>1,'Edit'=>1),
                'Attendance -Invited'=>array('View'=>1,'Edit'=>1),
                'Meetingtodo'=>array('Create'=>1,'View'=>1,'Edit'=>1,'Delete'=>1),
                'Minute -Draft mode'=>array('View'=>1),
                'Minute -Approved mode'=>array('View'=>1),
                'Minute approval'=>array('Create'=>0),
                'Report'=>array('View'=>1),
                'Decision'=>array('Create'=>1,'View'=>1,'Edit'=>1,'Delete'=>1),
                'Decision -Assigned'=>array('View'=>1,'Edit'=>1,'Delete'=>1),
                'Decision -Accepted'=>array('View'=>1,'Edit'=>0,'Delete'=>0),
                'Decision -Upcoming meeting'=>array('View'=>1),
                'Item'=>array('Create'=>1,'View'=>1,'Edit'=>1,'Delete'=>1),
                'Item -Involve'=>array('View'=>1,'Edit'=>1),
                'Userstatus'=>array('Create'=>1),
                'Userstatus -Own'=>array('Create'=>1),
                'Groupstatus'=>array('Create'=>1),
                'Groupstatus -Own'=>array('Create'=>1),
                
            );
        }else{
            $wfmodel=array(
                'Announcement'=>array('Create'=>0,'Edit'=>0,'Delete'=>0),
                'Meeting'=>array('View'=>1),
                'Meeting -Invited'=>array('View'=>1),
                'Meeting -Attended'=>array('View'=>1),
                'Attendance'=>array('View'=>0),
                'Attendance -Invited'=>array('View'=>0,'Edit'=>0),
                'Minute -Draft mode'=>array('View'=>0),
                'Minute -Approved mode'=>array('View'=>1),
                'Minute approval'=>array('Create'=>0),
                'Report'=>array('View'=>1),
                'Decision'=>array('View'=>0),
                'Decision -Assigned'=>array('View'=>1),
                'Decision -Accepted'=>array('View'=>1),
                'Decision -Upcoming meeting'=>array('View'=>1),
                'Item'=>array('View'=>0),
                'Item -Involve'=>array('View'=>1),
                'Userstatus'=>array('Create'=>0,),
                'Userstatus -Own'=>array('Create'=>1),
                'Groupstatus'=>array('Create'=>0),
                'Groupstatus -Own'=>array('Create'=>1),
            );
            if($role==3){
                $wfmodel['Minute -Draft mode']['View']=1;
            }
            return $wfmodel;
        }
    }
    
    function committee_role($committee_id,$role){
        $wfmodels=$this->defaultpermission($role);
        $this->recursive=-1;
        $committeewf=$this->find('all',array('conditions'=>array('committee_id'=>$committee_id,'role_id'=>$role)));
        foreach($committeewf as $cwf){
            $wfmodels[$cwf['Wfmodel']['model']][$cwf['Wfmodel']['crud']]=$cwf['Wfmodel']['permission'];
        }
        return $wfmodels;
    }
}
?>
